package com.itheima.edu.info.manager.day11;

import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

public class FileTest {
    public static void main(String[] args) throws IOException {
        // File file = new File("E://jinjie//实验//aaa//a.txt");
        File file = new File("D://steam");
        File file1 = new File("E://jinjie//实验//aaa//bbb//ccc");
//         System.out.println(mkdirs(file1));
        // System.out.println(file.getCanonicalPath());
        //  System.out.println(createNewFile(file));
        // System.out.println(delete(file1));
        TreeMap<String, Integer> hoZuiMing = getHoZuiMing(file);
        System.out.println(hoZuiMing.toString());
    }

    private static TreeMap<String, Integer> getHoZuiMing(File file) {
        TreeMap<String, Integer> tm = new TreeMap<>();
        int count = 1;
        //获取当前目录下的所有文件和文件夹并转换成对象存入数组中
        File[] files = file.listFiles();
        for (File file1 : files) {
            //当前对象为文件就为true
            if (file1.isFile()) {
                //当前对象所表示的路径转换成字符串
                String name = file1.getName();
                //以“.”分割字符串并存入数组
                String[] split = name.split("\\.");
                if (split.length == 2) {
                    String a = split[1];
                    if (tm.containsKey(a)) {
                        Integer integer = tm.get(a);
                        integer += count;
                        tm.put(a, integer);
                    } else {
                        tm.put(a, 1);
                    }
                } else if (split.length == 3) {
                    String a = split[1];
                    if (tm.containsKey(a)) {
                        Integer integer = tm.get(a);
                        integer += count;
                        tm.put(a, integer);
                    } else {
                        tm.put(a, 1);
                    }
                } else if (split.length == 4) {
                    String a = split[1];
                    if (tm.containsKey(a)) {
                        Integer integer = tm.get(a);
                        integer += count;
                        tm.put(a, integer);
                    } else {
                        tm.put(a, 1);
                    }
                }
            }
        }
        return tm;

    }


    //遍历当层文件夹里的所有文件和文件夹
    public static void listFiles(File file) {
        File[] listFiles = file.listFiles();
        for (File listFile : listFiles) {
            System.out.println(listFile);
        }
    }

    //创建文件
    private static boolean createNewFile(File file) {
        try {
            return file.createNewFile();
        } catch (IOException e) {
            System.out.println("错误");
            return false;
        }
    }

    private static boolean mkdirs(File file) {//创建文件夹或多个
        return file.mkdirs();
    }

    //所有当前文件夹里的所有文件夹和文件
    private static boolean delete(File file) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()) {
                file1.delete();
            } else {
                delete(file1);
            }

        }
        return file.delete();
    }
}
